package com.jming.jike.service.front.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.jming.jike.common.BusinessException;
import com.jming.jike.config.MybatisPlusConfig;
import com.jming.jike.dto.front.ShoppingCartDTO;
import com.jming.jike.entity.Dish;
import com.jming.jike.entity.ShoppingCart;
import com.jming.jike.mapper.DishMapper;
import com.jming.jike.mapper.ShoppingCartMapper;
import com.jming.jike.service.front.ShoppingCartService;
import com.jming.jike.util.CopyBeanUtils;
import com.jming.jike.vo.front.ShoppingCartDetailVO;
import com.jming.jike.vo.front.ShoppingCartVO;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 购物车(ShoppingCart)表服务实现类
 *
 * @author makejava
 * @since 2022-04-16 02:02:52
 */
@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {

    @Resource
    private DishMapper dishMapper;

    @Resource
    private ShoppingCartMapper shoppingCartMapper;

    @Resource
    private MybatisPlusConfig mybatisPlusConfig;

    @Override
    public Boolean add(ShoppingCartDTO shoppingCartDTO) {

        // 根据唯一id查询菜品信息
        Long soleId = shoppingCartDTO.getSoleId();
        Dish dish = dishMapper.queryMeal(soleId);
        if (ObjectUtils.isEmpty(dish)) {
            throw new BusinessException("菜品不存在或已下架,请联系相关人员处理!");
        }

        // 查询购物车是否存在当前菜品
        ShoppingCart shoppingCart = shoppingCartMapper.selectOne(Wrappers.<ShoppingCart>lambdaQuery()
                .eq(ShoppingCart::getUserId, mybatisPlusConfig.getOperId())
                .eq(ShoppingCart::getSoleId, soleId));

        // 如果购物车为空 则添加购物车
        if (ObjectUtils.isEmpty(shoppingCart)) {

            return shoppingCartMapper.insert(ShoppingCart.builder()
                    .name(dish.getName())
                    .imageUrl(dish.getImageUrl())
                    .soleId(dish.getSoleId())
                    .number(1)
                    .desc(shoppingCartDTO.getDesc())
                    .amount(dish.getPrice())
                    .build()) > 0;
        }

        // 如果购物车不为空 则将数量加1
        return shoppingCartMapper.updateById(ShoppingCart.builder()
                .id(shoppingCart.getId())
                .number(shoppingCart.getNumber() + 1)
                .amount(dish.getPrice().multiply(BigDecimal.valueOf(shoppingCart.getNumber() + 1)))
                .build()) > 0;
    }

    @Override
    public Boolean cut(ShoppingCartDTO shoppingCartDTO) {

        // 菜品唯一id
        Long soleId = shoppingCartDTO.getSoleId();

        // 查询购物车是否存在当前菜品
        ShoppingCart shoppingCart = shoppingCartMapper.selectOne(Wrappers.<ShoppingCart>lambdaQuery()
                .eq(ShoppingCart::getUserId, mybatisPlusConfig.getOperId())
                .eq(ShoppingCart::getSoleId, soleId));

        // 如果不存在 则直接返回true
        if (ObjectUtils.isEmpty(shoppingCart)) {
            return true;
        }

        // 查询在售的菜品信息
        Dish dish = dishMapper.queryMeal(soleId);
        // 如果在售菜品不存在 则直接删除购物车
        // 如果菜品在售菜品存在, 则查看数量是否为1 如果为1 则直接删除商品 如果不为1 则减一
        if (ObjectUtils.isEmpty(dish) || Integer.valueOf(1).equals(shoppingCart.getNumber())) {

            return shoppingCartMapper.deleteById(shoppingCart.getId()) > 0;
        }

        return shoppingCartMapper.updateById(ShoppingCart.builder()
                .id(shoppingCart.getId())
                .number(shoppingCart.getNumber() - 1)
                .amount(dish.getPrice().multiply(BigDecimal.valueOf(shoppingCart.getNumber() - 1)))
                .build()) > 0;
    }

    @Override
    public Boolean clean() {

        return shoppingCartMapper.delete(Wrappers.<ShoppingCart>lambdaQuery()
                .eq(ShoppingCart::getUserId, mybatisPlusConfig.getOperId())) > 0;
    }

    @Override
    public ShoppingCartDetailVO list() {

        // 当前购物车商品
        List<ShoppingCart> shoppingCartList = shoppingCartMapper.selectList(Wrappers.<ShoppingCart>lambdaQuery()
                .eq(ShoppingCart::getUserId, mybatisPlusConfig.getOperId())
                .orderByDesc(ShoppingCart::getCreateTime));

        // 如果购物车为空 直接返回空
        if (ObjectUtils.isEmpty(shoppingCartList)) {
            return null;
        }

        // 得到菜品唯一id
        List<Long> soleIdList = shoppingCartList.stream().map(ShoppingCart::getSoleId).collect(Collectors.toList());

        // 查询购物车里面在售的菜品
        List<Long> existSoleIdList = dishMapper.queryMealList(soleIdList);

        // 移除在售的菜品
        // 如果还有菜品唯一id 则说明存在停售的菜品 删除停售的菜品
        soleIdList.removeAll(existSoleIdList);
        if (soleIdList.size() != 0) {

            shoppingCartMapper.delete(Wrappers.<ShoppingCart>lambdaQuery()
                    .eq(ShoppingCart::getUserId, mybatisPlusConfig.getOperId())
                    .in(ShoppingCart::getSoleId, soleIdList));
        }

        // 过滤掉停售的菜品
        shoppingCartList = shoppingCartList.stream().filter(scl -> existSoleIdList.contains(scl.getSoleId())).collect(Collectors.toList());

        ShoppingCartDetailVO shoppingCartDetailVO = new ShoppingCartDetailVO();
        shoppingCartDetailVO.setShoppingCartVOList(CopyBeanUtils.copyBeanList(shoppingCartList, ShoppingCartVO.class));
        // 计算总数量
        shoppingCartDetailVO.setTotalNumber(shoppingCartList.stream().map(ShoppingCart::getNumber).reduce(0, Integer::sum));
        // 计算总金额
        shoppingCartDetailVO.setTotalAmount(shoppingCartList.stream().map(ShoppingCart::getAmount).reduce(BigDecimal.ZERO, BigDecimal::add));

        return shoppingCartDetailVO;
    }
}
